/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import org.jbfilter.core.CleaningStrategy;
import org.jbfilter.core.EnableStrategy;
import org.jbfilter.core.FilterComponent;

abstract class _FilterComponentImpl<E> implements FilterComponent<E> {
	// members
	private String id;
	private boolean enabled = true;
	private EnableStrategy enableStrategy;
	private boolean inverted = false;
	private CleaningStrategy cleaningStrategy;
	
	// constructors
	_FilterComponentImpl() {
		setCleaningStrategy(getDefaultCleaningStrategy());
	}
	_FilterComponentImpl(String id) {
		this();
		this.id = id;
	}
	protected abstract CleaningStrategy getDefaultCleaningStrategy();
	// methods
	@Override
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	@Override
	public boolean isEnabled() {
		return enabled;
	}
	@Override
	public boolean isActive() {
		return enabled && (enableStrategy == null || enableStrategy.decideEnabled());
	}
	@Override
	public void clean() {
		if (cleaningStrategy != null) {
			cleaningStrategy.clean();
		}
	}
	@Override
	public void setCleaningStrategy(CleaningStrategy cleaningStrategy) {
		this.cleaningStrategy = cleaningStrategy;
	}
	@Override
	public String getId() {
		return id;
	}
	@Override
	public void setId(String id) {
		this.id = id;
	}
	@Override
	public boolean isInverted() {
		return inverted;
	}
	@Override
	public void setInverted(boolean inverted) {
		this.inverted = inverted;
	}
	@Override
	public void setEnableStrategy(EnableStrategy enableStrategy) {
		this.enableStrategy = enableStrategy;
	}
	@Override
	public CleaningStrategy getCleaningStrategy() {
		return cleaningStrategy;
	}
	@Override
	public EnableStrategy getEnableStrategy() {
		return enableStrategy;
	}
}
